/**
 * 
 */
package com.gr.staffpm.utils;

import java.util.Date;

import org.joda.time.DateTime;
import org.joda.time.Instant;
import org.joda.time.Period;
import org.joda.time.PeriodType;
import org.joda.time.format.PeriodFormatter;
import org.joda.time.format.PeriodFormatterBuilder;


/**
 * @author Graham Rhodes 4 May 2011 23:44:43
 */
public class DateTimeUtils {

    public static boolean isToday(DateTime date) {
        DateTime now = new DateTime();
        boolean sameDay = date.getDayOfMonth() == now.getDayOfMonth();
        boolean sameMonth = date.getMonthOfYear() == now.getMonthOfYear();
        boolean sameYear = date.getYear() == now.getYear();
        return sameDay && sameMonth && sameYear;
    }

    public static boolean isThisWeek(DateTime date) {
        DateTime startOfWeek = new DateTime();
        if (startOfWeek.getDayOfWeek() > 1)
            startOfWeek = startOfWeek.minusDays(startOfWeek.getDayOfWeek());
        return date.isAfter(startOfWeek) && date.isBefore(startOfWeek.plusDays(7));
    }

    /**
     * Gets a human readable format relative to System Date Time
     * e.g. 1 day, 5 minutes ago
     * @param date
     * @return
     */
    public static String toHumanFormat(Date date) {
        return toHumanFormat(new DateTime(date));
    }

    /**
     * Gets a human readable format relative to System Date Time
     * e.g. 1 day, 5 minutes ago
     * @param date
     * @return
     */
    public static String toHumanFormat(DateTime timestamp) {
        PeriodFormatter periodFormat =
                new PeriodFormatterBuilder().printZeroNever().appendWeeks().appendSuffix(" week", " weeks").appendSeparator(", ").appendDays().appendSuffix(" day", " days").appendSeparator(
                        ", ").appendHours().appendSuffix(" hour", " hours").appendSeparator(", ").appendMinutes().appendSuffix(" minute", " minutes").appendSeparator(", ").appendSeconds().appendSuffix(
                        " second", " seconds").toFormatter();
        return periodFormat.print(new Period(timestamp, new Instant(), PeriodType.standard())) + " ago";
    }
}
